package com.yomahub.liteflow.example.xstream.parameterFileDemo;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.security.AnyTypePermission;
import com.yomahub.liteflow.example.xstream.parameterFileDemo.base.*;

/**
 * @program: freemarker-project
 * @description:
 * @author: yyyyjinying
 * @create: 2023-11-09 23:51
 **/
public class xmlToObjectClient {
    public static void main(String[] args) {
        String xmlStr = "<parameterFile>\n" +
                "  <config>\n" +
                "    <header version=\"1.0\" wordsize=\"1024\" synch1=\"583\" synch2=\"1464\" synch3=\"2631\" synch4=\"3512\" title=\"737-7\" memo=\"737-7译码配置xml文件\"/>\n" +
                "    <superFrameCounter word=\"499\" olsb=\"9\" omsb=\"8\" subframe=\"7\"/>\n" +
                "  </config>\n" +
                "  <parameters>\n" +
                "    <parameter code=\"900001\" rate=\"1\" name=\"DATETIME\" sign=\"N\" type=\"DAT\" memo=\"Date Time\" insertion=\"1\">\n" +
                "      <locations>\n" +
                "        <location omsb=\"12\" supframe=\"2\" word=\"256\" olsb=\"8\" id=\"1\" subframe=\"1\"/>\n" +
                "      </locations>\n" +
                "      <info valuemap=\"11:H32,1:H3,12:1 2 0\"/>\n" +
                "      <expressions>\n" +
                "        <expression minRawValue=\"999999\" maxRawValue=\"0.004880000\" c1=\"999999\"/>\n" +
                "      </expressions>\n" +
                "    </parameter>\n" +
                "  </parameters>\n" +
                "</parameterFile>";

        // 创建XStream
        XStream xstream = new XStream(new DomDriver("UTF-8"));

//        放开权限
        xstream.addPermission(AnyTypePermission.ANY);

        xstream.alias("parameterFile", ParameterFile.class);
        xstream.alias("parameter", Parameter.class);
        xstream.alias("location", Location.class);
        xstream.alias("expression", Expression.class);
        xstream.useAttributeFor(Header.class, "version");
        xstream.useAttributeFor(Header.class, "wordsize");
        xstream.useAttributeFor(Header.class, "synch1");
        xstream.useAttributeFor(Header.class, "synch2");
        xstream.useAttributeFor(Header.class, "synch3");
        xstream.useAttributeFor(Header.class, "synch4");
        xstream.useAttributeFor(Header.class, "title");
        xstream.useAttributeFor(Header.class, "memo");

        xstream.useAttributeFor(SuperFrameCounter.class, "word");
        xstream.useAttributeFor(SuperFrameCounter.class, "olsb");
        xstream.useAttributeFor(SuperFrameCounter.class, "omsb");
        xstream.useAttributeFor(SuperFrameCounter.class, "subframe");


        xstream.useAttributeFor(Parameter.class, "code");
        xstream.useAttributeFor(Parameter.class, "rate");
        xstream.useAttributeFor(Parameter.class, "name");
        xstream.useAttributeFor(Parameter.class, "sign");
        xstream.useAttributeFor(Parameter.class, "type");
        xstream.useAttributeFor(Parameter.class, "memo");
        xstream.useAttributeFor(Parameter.class, "insertion");
        xstream.useAttributeFor(Location.class, "omsb");
        xstream.useAttributeFor(Location.class, "supframe");
        xstream.useAttributeFor(Location.class, "word");
        xstream.useAttributeFor(Location.class, "olsb");
        xstream.useAttributeFor(Location.class, "id");
        xstream.useAttributeFor(Location.class, "subframe");
        xstream.useAttributeFor(Info.class, "valuemap");

        xstream.useAttributeFor(Expression.class, "minRawValue");
        xstream.useAttributeFor(Expression.class, "maxRawValue");
        xstream.useAttributeFor(Expression.class, "c1");

//        xStream.autodetectAnnotations(true);
        // 对象转化时需要手动扫描注解(自动不生效)
        // xStream.processAnnotations(ParameterFile.class);
        ParameterFile parameterFile = (ParameterFile) xstream.fromXML(xmlStr);
        System.out.println(parameterFile.getConfig().getHeader());
    }
}
